package com.hunliji.monitor.share.utils;

import com.hunliji.common.utils.DateUtil;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 * @author wuchen
 */
public class DateCommonUtil {
    /**
     * 一天的毫秒数
     */
    private static final Long ONE_DAY_MILLION_SECOND = 86400000L;
    /**
     * 一秒的毫秒数
     */
    private static final Long ONE_SECOND_MILLION_SECOND = 1000L;
    /**
     * 获取在一年中的第几天
     * 这个天的起始标记为0，因此全部获取的天都需要-1
     * @param date
     * @return
     */
    public static Integer getDayToYear(Date date){
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        Integer i = ca.get(Calendar.DAY_OF_YEAR);
        i = i-1;
        return i;
    }

    /**
     * 获取当天的时间零点
     * @return
     */
    public static Date getTodayZero(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Long todayZero = calendar.getTimeInMillis();
        return new Date(todayZero);
    }

    /** 获得某天的零点时刻0:0:0 */
    public static Date getDayBegin(Date date) {
        if (date == null) {
            return null;
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /** 获得某天的截至时刻23:59:59 */
    public static Date getDayEnd(Date date) {
        if (date == null) {
            return null;
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    /**
     * 根据当前时间获取前几天的同一时间
     * @param currentDate：当前时间
     * @param day：天
     * @return
     */
    public static  Date getBeforeDayCurrent(Date currentDate,Integer day){
        //当前的毫秒
        Long currentTimestamp = currentDate.getTime();
        //目标的毫秒
        Long targetTimestamp = currentTimestamp - ONE_DAY_MILLION_SECOND * day;
        return new Date(targetTimestamp);
    }


    /**
     * 根据当前时间获取前几秒的时间
     * @param currentDate：当前时间
     * @param second：秒数
     * @return
     */
    public static  Date getBeforeSecondCurrent(Date currentDate,Integer second){
        //当前的毫秒
        Long currentTimestamp = currentDate.getTime();
        //目标的毫秒
        Long targetTimestamp = currentTimestamp - ONE_SECOND_MILLION_SECOND * second;
        return new Date(targetTimestamp);
    }
}
